/**
 * 
 */
package ivan.cheng.tuqu.adapter;

import ivan.cheng.tuqu.R;
import ivan.cheng.tuqu.util.BitmapUtil;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * @author ivan
 *
 */
public class GridScrawlAdapter extends BaseAdapter{

	/**
	 * 
	 */
	private Context mContext;
	public String[] acceLst;
	private List<WeakReference<Bitmap>> mBitmapLst;
	private LayoutInflater mInflater;
	public GridScrawlAdapter(Context _context,List<WeakReference<Bitmap>> bitmapLst) {
		mContext = _context;
		mInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		try {
			acceLst = mContext.getAssets().list("scrawl");
		} catch (IOException e) {
			e.printStackTrace();
		}
		mBitmapLst = bitmapLst;
	}

	public int getCount() {
		return acceLst.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ImageView i;
		TextView t;
        if (convertView == null) {
        	convertView = mInflater.inflate(R.layout.item_effect, null);
            convertView.setLayoutParams(new Gallery.LayoutParams(parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
					5, mContext.getResources().getDisplayMetrics()), 
					parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
	    					5, mContext.getResources().getDisplayMetrics())));
        } /*else {
            i = (ImageView) convertView;
        }*/
        i = (ImageView)convertView.findViewById(R.id.IV_image);
        i.setLayoutParams(new LinearLayout.LayoutParams(parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
				30, mContext.getResources().getDisplayMetrics()), 
				parent.getHeight() - (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
    					30, mContext.getResources().getDisplayMetrics())));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        t = (TextView)convertView.findViewById(R.id.TV_text);
        t.setText(mContext.getResources().getStringArray(R.array.effect_scraw_thumb_name_items)[position]);
        Bitmap bmp = (Bitmap)i.getTag(R.id.image_object);
        if(mBitmapLst.size() > position && null != mBitmapLst.get(position))
        	bmp = mBitmapLst.get(position).get();
        if(null == bmp)
        {
        	try {
    			
    			bmp = BitmapUtil.instance().DecodeFromStream(mContext.getAssets().open("scrawl/" + acceLst[position]));
    		} catch (IOException e) {
    			
    			e.printStackTrace();
    		}
    		mBitmapLst.add(new WeakReference<Bitmap>(bmp));
        }
		
		//i.setPadding(2, 2, 2, 2);
		i.setBackgroundResource(R.drawable.beauty_element_frame);
		i.setImageBitmap(bmp);
		//i.setTag(R.id.image_object, bmp);
        return convertView;
	}

}
